import { defineStore } from "pinia";
import { ref } from "vue";
import { getListForUser } from "../apis/menu/menu";
import { Menu,MenuTree } from "../apis/menu/type";
import { buildTree } from "../utils/treeUtil";

 export const useMenuStore =defineStore("menu",()=>{

    //左侧菜单
     const leftMenus=ref<MenuTree[]>([]);

     const roleButtonIds=ref<number[]>([]);


      //路由页面
     const routerMenus=ref<MenuTree[]>([]);

     /**
      * 获取用户权限
      */
     const getMenus=async ()=>{


        if(leftMenus.value.length>0) return;

        console.log("调接口");
        

        const res=await getListForUser();

        roleButtonIds.value= res.list.map((item)=>item.id);//权限id

        console.log("权限id",roleButtonIds.value);
        

        console.log("aaaa",res);
        

         let _leftMenus= res.list.filter((item)=>item.isButton==0);
         leftMenus.value=  buildTree(_leftMenus).filter(item=>item.children.length>0);

         let _routerMenus= res.list.filter((item)=>item.isButton==0 || item.pathName);
         routerMenus.value=  buildTree(_routerMenus);
 


     }

     return {leftMenus,routerMenus,getMenus,roleButtonIds}

     

}
,{
    persist:{
        storage: localStorage,
    }
}
)